package com.hhd.flow.service.impl;


import com.hhd.flow.vo.ListenerVo;
import com.hhd.flow.engine.FlowEngine;
import com.hhd.flow.engine.lisener.impl.DefaultFlowProcessEventMultiCaster;
import com.hhd.flow.service.FlowListenerService;
import com.hhd.flow.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ListenerService implements FlowListenerService {

    @Autowired
    FlowEngine flowEngine;

    @Override
    public List<ListenerVo> loadListeners(String listenerType, String formType) {
        List<DefaultFlowProcessEventMultiCaster.ParsedLisener> listeners = flowEngine.getParsedLiseners();
        if (CommonUtils.isEmpty(listeners)) return CommonUtils.newList();

        return listeners.stream().filter(flowListener -> {
            return CommonUtils.isMatch(flowListener.getFormType(), formType) && !flowListener.isPreset();
        }).map(flowListener -> {
            ListenerVo listenerVo = new ListenerVo();
            listenerVo.setTitle(flowListener.getListenerTitle());
            listenerVo.setValue(flowListener.getListenerName());
            return listenerVo;
        }).collect(Collectors.toList());
    }
}
